package listbyorder.access001_100.test61;

import listbyorder.utils.ListNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/3 10:25
 */
public class Solution1 {

    // 方法一； 首先将聊表构成环，然后旋转一定长度即可
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0) return head;
        ListNode temp = head;
        int len = 1;
        while (temp.next != null) {
            len++;
            temp = temp.next;
        }
        if (len == k) return head;
        temp.next = head;
        int step = len - (k % len);
        while (step > 0) {
            temp = temp.next;
            step--;
        }
        ListNode res = temp.next;
        temp.next = null;
        return res;
    }
}
